package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.CheckScanTable;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/17 0017
 *
 * 盘点扫描管理数据表的操作
 */

public class CheckScanTableDao extends BaseDao<CheckScanTable, Long> {

  public CheckScanTableDao() {
    super(DbHelper.getInstance(BaseApplication.getContext()), CheckScanTable.class);
  }

  /**
   * 查询对应子单是否扫描了
   */
  public int findChildWayBillNoStatus(String stockNo, String waybillNo, String waybillChildNo) {
    int status = -1;
    try {
      QueryBuilder<CheckScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("stockNo", stockNo).and().eq("waybillNo", waybillNo).and().eq("waybillChildNo", waybillChildNo);

      List<CheckScanTable> checkScanTableList = queryBuilder.query();
      if (checkScanTableList != null && checkScanTableList.size() > 0) {
        status = checkScanTableList.get(0).getScanStatus();
      }
      return status;
    } catch (SQLException e) {
      e.printStackTrace();
      status = -1;
    }
    return status;
  }

  /**
   * 判断子单是否存在
   */
  public boolean exitChildWaybillNoData(String stockNo, String childWaybillNo) {
    try {
      QueryBuilder<CheckScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("stockNo", stockNo).and().eq("waybillChildNo", childWaybillNo);

      CheckScanTable checkScanTable = queryBuilder.queryForFirst();
      if (checkScanTable == null) {
        return false;
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return true;
  }

  /**
   * 获取盘点子单
   */
  public CheckScanTable getChildWaybillNoData(String stockNo, String childWaybillNo) {
    try {
      QueryBuilder<CheckScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("stockNo", stockNo).and().eq("waybillChildNo", childWaybillNo);

      CheckScanTable checkScanTable = queryBuilder.queryForFirst();
      return checkScanTable;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据盘点号和运单号查询数据
   */
  public List<CheckScanTable> getWaybillNoData(String stockNo, String waybillNo) {
    try {
      QueryBuilder<CheckScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("stockNo", stockNo).and().eq("waybillNo", waybillNo);

      List<CheckScanTable> list = queryBuilder.query();
      return list;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据盘点好查询数据
   */
  public List<CheckScanTable> getDataByStockNo(String stockNo) {
    try {
      QueryBuilder<CheckScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("stockNo", stockNo);
      List<CheckScanTable> list = queryBuilder.query();
      return list;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 查询未上传的数据
   */
  public List<CheckScanTable> getUnUploadDataList() {
    try {
      QueryBuilder<CheckScanTable, Long> queryBuilder = dao.queryBuilder();
      /**
       * 查询字段“isUpload”等于0的条件数据，
       *
       * 相当于sql语句"select * from 'buildPckScanTable' where 'isUpload' == 0 "
       *
       */
      List<CheckScanTable> buildPckScanListData = queryBuilder.where().eq("isUpload", 0).query();

      return buildPckScanListData;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 更新表数据对象
   */
  public int updataItem(CheckScanTable itemTable) {
    int result = -1;
    try {
      result = this.update(itemTable);
    } catch (SQLException e) {
      e.printStackTrace();
      result = -1;
    }
    return result;
  }

  /**
   * 查询未上传的数量
   */
  public int queryNotUploadDataNum() {
    int totalNum = 0;
    List<CheckScanTable> mTableList;
    try {
      mTableList = super.findAll();
      if (mTableList != null && mTableList.size() > 0) {
        for (CheckScanTable itemTable : mTableList) {
          int isUpload = itemTable.getIsUpload();
          if (isUpload == 0) {
            totalNum++;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return totalNum;
  }
}
